import Mock from 'mockjs'
import blogText from './blogDetail'
import { baseURL } from './config'
import getName from '@/utils/mockName'
type mockListType = {
  api: string
  [propName: string]: any
}

function setMock() {
  for (const el of mockList) {
    const data: any = {}
    for (const key in el) {
      if (key !== 'api') {
        data[key] = el[key]
      }
    }
    Mock.mock(baseURL + el.api, {
      mock: true,
      status: true,
      code: 0,
      ...data,
    })
  }
  Mock.setup({
    timeout: '10-300',
  })
}
const mockList: mockListType[] = [
  {
    api: 'getBlogList',
    'data|30': [
      {
        creationTime: '@date(T)', //随机生成日期时间
        content: '@cword(100)',
        title: '@cword(10)',
        'id|+1': 1,
        cover: '@dataImage(200x200)',
        viewCount: '@integer(200,10000)',
      },
    ],
    code: 2,
  },
  {
    api: 'addBlog',
  },
  {
    api: 'getBlogDetail',
    data: {
      creationTime: '@date(T)', //随机生成日期时间
      content: blogText,
      title: '@cword(10)',
      cover: '@dataImage(200x200)',
      viewCount: '@integer(200,10000)',
      'commentList|0-10': [
        {
          'commentId|+1': 1,
          content: '@cword(100)',
          reply: '@csentence(0,30)',
          creatTime: '@date(T)',
          name: '@cname',
        },
      ],
    },
  },
  {
    api: 'queryBlog',
    'data|3': [
      {
        creationTime: '@date(T)', //随机生成日期时间
        content: '@cword(100)',
        title: '@cword(10)',
        'id|+1': 1,
        cover: '@dataImage(200x200)',
        viewCount: '@integer(200,10000)',
      },
    ],
  },
  {
    api: 'addViewCount',
  },
  {
    api: 'addComment',
    data: {
      commentId: () => Mock.mock('@integer(200,10000)'),
      name: () => getName(),
      creatTime: () => String(new Date().getTime()),
      reply: '',
    },
  },
  {
    api: 'queryComment',
    'data|3-10': [
      {
        'commentId|+1': 1,
        content: '@cword(100)',
        // reply: '@csentence(0,30)',
        reply: '',
        creatTime: '@date(T)',
        name: '@cname',
        blogId: '@integer(1-10000)',
        blogTitle: '@cword(10)',
        replyTime: '@date(T)',
      },
    ],
  },
  {
    api: 'getInfo',
  },
  {
    api: 'getToken',
    data: {
      token: 'mocktoken',
    },
  },
  {
    api: 'deleteBlog',
  },
  {
    api: 'deleteComment',
  },
  {
    api: 'deleteCommentReply',
  },
  {
    api: 'addCommentReply',
  },
  {
    api: 'getTotalCommentStatus',
    data: {
      num: '@integer(0,3)',
    },
  },
]
mockList.length = 0

setMock()
